﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HSIA
{
    public partial class FrmAccountEdit : Form
    {
        public FrmAccountEdit(DatabaseObject dbo, DataTable dtAccount)
        {
            this.dbo = dbo;
            this.dtAccount = dtAccount;
            InitializeComponent();
        }

        DataTable dtAccount;
        DatabaseObject dbo;
        string sKeyId;
        string strConn = "";

        private void FrmAccountEdit_Load(object sender, EventArgs e)
        {
            cbIp.DataSource = dtAccount;
            cbIp.DisplayMember = "Ip";
            cbIp.ValueMember = "Ip";

            cbDB_User.DataSource = dtAccount;
            cbDB_User.DisplayMember = "DB_User";
            cbDB_User.ValueMember = "DB_User";

            sKeyId = Guid.NewGuid().ToString();

            tbCompany_Code.Text = sKeyId.Substring(0, 4);
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            if (!CheckData()) return;
            string sInsertSQL = "";//" + sWarnFieldValue + 
            string sKey = "", sValue = "";
            DataRow drAccount = dtAccount.NewRow();
            foreach (Control c in this.Controls)
            {
                if ((c.Name.StartsWith("tb") || c.Name.StartsWith("cb")) && c.Enabled)
                {
                    string sColumnID = c.Name.Substring(2, c.Name.Length - 2);
                    drAccount[sColumnID] = c.Text;
                    sKey += sColumnID + ",";
                    sValue += "'" + c.Text + "',";
                }
            }
            if (sKey != "" && sValue != "")
            {
                drAccount["KeyId"] = sKeyId;
                sKey += "KeyId,";
                sValue += "'" + sKeyId + "',";

                string sCreateDate = DateTime.Now.ToString();
                drAccount["CreateDate"] = sCreateDate;
                sKey += "CreateDate,";
                sValue += "'" + sCreateDate + "',";

                dtAccount.Rows.Add(drAccount);
                if (BuildAccount(drAccount))
                {
                    sInsertSQL = string.Format("insert into Sys_Account({0}) values({1})", sKey.TrimEnd(','), sValue.TrimEnd(','));
                    string str = dbo.ExcuteSQL(sInsertSQL);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
        }

        private bool CheckData()
        {
            bool b = true;
            int iValue = 0;
            foreach (Control c in this.Controls)
            {
                if (c.Enabled && c.Tag != null && c.Text.Trim() == "")
                {
                    MessageBox.Show(string.Format(dbo.GetLanguage(1), dbo.GetLanguage(c.Tag)), dbo.GetLanguage(141), MessageBoxButtons.OK, MessageBoxIcon.Warning);//" 区域信息不能为空,请选择或输入!");
                    c.Focus();
                    return false;
                }
                //if (c is TextBox && int.TryParse(c.Text.Trim(), out iValue))
                //{
                //    c.Text = iValue.ToString();
                //}
            }

            foreach (DataRow dr in dtAccount.Rows)
            {
                if (dr["Company_Code"].ToString() == tbCompany_Code.Text.Trim())
                {
                    MessageBox.Show("已存在公司代码，请重新输入！", dbo.GetLanguage(141), MessageBoxButtons.OK, MessageBoxIcon.Warning);//" 区域信息不能为空,请选择或输入!");
                    tbCompany_Code.Focus();
                    return false;
                }
                if (dr["Company_Name"].ToString() == tbCompany_Name.Text.Trim())
                {
                    MessageBox.Show("已存在公司名称，请重新输入！", dbo.GetLanguage(141), MessageBoxButtons.OK, MessageBoxIcon.Warning);//" 区域信息不能为空,请选择或输入!");
                    tbCompany_Name.Focus();
                    return false;
                }
            }
            return b;
        }

        private bool BuildAccount(DataRow drAccount)
        {
            bool b = true;
            string strConnect = string.Format(DatabaseObject.sMasterConnect,
                                            drAccount["IP"], drAccount["DB_Name"], drAccount["DB_User"], drAccount["DB_Password"]);
            string sInsertSQL = dbo.ExcuteSQLFile(Application.StartupPath + "/database/amate_0000.sql", strConnect);
            if (sInsertSQL != "OK")
            {
                MessageBox.Show(sInsertSQL, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                b = false;
            }
            return b;
        }

        private void cbDB_Name_DropDown(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbIp.Text.Trim()) ||
                string.IsNullOrEmpty(cbDB_User.Text.Trim()) ||
                string.IsNullOrEmpty(tbDB_Password.Text.Trim()))
                return;
            string strConnect = string.Format(DatabaseObject.sMasterConnect,
                                            cbIp.Text.Trim(), "master", cbDB_User.Text.Trim(), tbDB_Password.Text.Trim());

            if (strConn == strConnect) return;
            strConn = strConnect;
            DataTable dtDB = dbo.GetDataTableBySql("Select name as DB_Name From master..sysdatabases Where name like 'amate%'", "DB_Name", strConn);
            cbDB_Name.DataSource = dtDB;
            if (dtDB.Rows.Count > 0)
            {
                cbDB_Name.DisplayMember = "DB_Name";
                cbDB_Name.ValueMember = "DB_Name";
            }

        }

    }
}
